layui.config({
    base: '/admin/js/module/'
}).extend({
    dialog: 'dialog',
});
layui.use(['form','jquery', 'layer',  'dialog'], function() {
    var form = layui.form(),
        layer = layui.layer,
        $ = layui.jquery,
        dialog = layui.dialog;
    //渲染表单
    form.render();
    //顶部添加
    $('.addBtn').click(function() {
        var url=$(this).attr('data-url');
        var str=$(this).attr('data-str');
        //将iframeObj传递给父级窗口,执行操作完成刷新
        layer.open({
            type: 2,
            title: str,
            area: ['700px', '620px'],
            fixed: false, //不固定
            content: url
        });
        return false;
    }).mouseenter(function() {
        dialog.tips('添加用户', '.addBtn');
    });

    //编辑系统用户
    $('#table-list').on('click', '.edit-btn', function() {
        var That = $(this);
        var url = That.attr('data-url');
        //将iframeObj传递给父级窗口
        layer.open({
            type: 2,
            title: "系统用户编辑",
            area: ['700px', '620px'],
            fixed: false, //不固定
            content: url
        });
        return false;
    });

    //修改密码
    $('#table-list').on('click', '.edit-pass', function() {
        var id = $(this).attr("data-id");
        layer.prompt({
            formType: 0,
            value: '',
            title: '请为用户设置新密码',
            area: ['800px', '350px'] //自定义文本域宽高
        },function(value, index, elem){
            var patn = /^(\w){6,20}$/;
            if (!patn.exec(value)){
                layer.msg("密码必须6-20位", {shade: 0.1,time: 1000});
                return;
            }
            $.ajax({
                url:'/admin/update/sys/user/pass',
                method:'post',
                data:{"id":id,"pass":value},
                dataType:'json',
                success:function (result) {
                    if(result.success){
                        layer.msg("操作成功", {shade: 0.3});
                        layer.close(index);
                    }else{
                        layer.msg(result.message, {shade: 0.3,time:1000});
                    }
                },
                error:function () {
                    layer.msg("系统错误，请稍后再试", {shade: 0.3,time:1000});
                }
            })

        });
    });


    //修改状态
    $('#table-list').on('click', '.edit-status', function() {
        var status = $(this).attr("data-status");
        var id = $(this).attr("data-id");
        var message = status == 0 || status == 1 ? status==0 ? '恢复' : '冻结' :'删除';
        dialog.confirm({
            message:'您确定要' + message + '此用户吗',
            success:function(){
                $.ajax({
                    url:'/admin/update/sys/user/status',
                    method:'post',
                    data:{"id":id,"status":status},
                    dataType:'json',
                    success:function (result) {
                        if(result.success){
                            layer.msg("操作成功", {shade: 0.3});
                            setTimeout(function () {
                                test();
                            },1000);
                        }else{
                            layer.msg(result.message, {shade: 0.3,time:1000});
                        }
                    },
                    error:function () {
                        layer.msg("系统错误，请稍后再试", {shade: 0.3,time:1000});
                    }
                })
            }
        })
    });
});

function test() {
   $("#sysUserForm").submit();
}

